package utils

import (
	"io"
	"os"
	"path/filepath"
	"time"

	log "github.com/sirupsen/logrus"
)

func InitLogger() {
	InitLoggerWithFileName("")
}
func InitLoggerWithFileName(filename string) {
	log.SetFormatter(&log.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05.000",
	})
	err := os.MkdirAll("log", os.ModePerm)
	if err != nil {
		log.Fatalf("create log dir failed: %v", err)
	}
	logFileName := filename + time.Now().Format("20060102_150405") + ".log"
	fp := filepath.Join("log", logFileName)
	f, err := os.Create(fp)
	if err != nil {
		log.Fatalf("create log file %s failed: %v", fp, err)
	}
	log.SetOutput(io.MultiWriter(os.Stdout, f))
}
